package kr.pe.kiwook.widget.alarmmanager;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

public class Widget_AlarmManager extends AppWidgetProvider {

	public static final String WIDGETUPDATE = "kr.pe.kiwook.widget.UPDATE";
	public static final String BUTTONACTION = "kr.pe.kiwook.widget.BUTTONACTION";
	public static RemoteViews rViews;
	public static int i = 0; 
	
	
	@Override
	public void onEnabled(Context context) {
		// TODO Auto-generated method stub
		super.onEnabled(context);
		rViews = new RemoteViews(context.getPackageName(),R.layout.widget);
	}
	
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		super.onReceive(context, intent);
		
		if(intent.getAction().equalsIgnoreCase(WIDGETUPDATE)){
			// Widget Update Action
			Log.d("Widget_AlarmManager","onReceive_WIDGETUPDATE");
			UpdateWidget(context);
			
		}else if(intent.getAction().equalsIgnoreCase(BUTTONACTION)){
			// Widget Button Action
			Log.d("Widget_AlarmManager","onReceive_BUTTONACTION");
			Toast.makeText(context, "Click!!", Toast.LENGTH_SHORT).show();
			
			Intent i = new Intent(context, Main.class);
			PendingIntent p = PendingIntent.getActivity(context, 0, i, 0);
			try{
				p.send(context, 0, i);
			}catch(Exception e){
				Log.d("Widget_AlarmManager",e.toString());
			}
		}
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		// TODO Auto-generated method stub
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		Log.d("Widget_AlarmManager","Update");
		
		// set Alarm
		Intent intent = new Intent(WIDGETUPDATE);
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 1000, pendingIntent);
		
		// set Button		
		Intent intentBtnAction = new Intent(BUTTONACTION);
		PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, intentBtnAction, PendingIntent.FLAG_UPDATE_CURRENT);
		rViews.setOnClickPendingIntent(R.id.btnOK, actionPendingIntent);
		
	}
	
	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		// TODO Auto-generated method stub
		super.onDeleted(context, appWidgetIds);
		
		// unregi.. alarm
		Intent intent = new Intent(WIDGETUPDATE);
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		alarmManager.cancel(pendingIntent);
		
		Log.d("Widget_AlarmManager","onDeleted");
		
	}
	
	public void UpdateWidget(Context con){
		
		Log.d("Widget_AlarmManager","UpdateWidget");
		
		i++;
		
		ComponentName thisWidget = new ComponentName(con, Widget_AlarmManager.class);
		
		//update views on remoteView
		rViews.setTextViewText(R.id.tvTitle, Integer.toString(i));
		AppWidgetManager manager = AppWidgetManager.getInstance(con);
		
		//update wiget
		manager.updateAppWidget(thisWidget, rViews);
		
		
	}

}
